package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.mapper.ProductSortMapper;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.ProductQueryObject;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private ProductSortMapper productSortMapper;


    //查询所有产品
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll() {
        //将数据转换成json格式
        List<Product> products = productService.selectAll();
        return products;
    }

    @RequestMapping("queryForList")
    @ResponseBody
    public Object queryForList(@ModelAttribute("qo") ProductQueryObject qo, Model model) {
        if (qo.getOne()!=null) {
            Long newOne = productSortMapper.selectBySn(qo.getOne());
            qo.setNewOne(newOne);
        }
        //将数据转换成json格式
        PageResult result = productService.queryForList(qo);
        return result;
    }

    //仅用以转发到product.jsp界面
    @RequestMapping("view")
    public String view(Model model, Long id) {
        return "/product/product";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate( Product entity) {
       productService.saveOrUpdate(entity);

        return new JSONResult();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete( Long id) {
        productService.deleteByPrimaryKey(id);
        return new JSONResult();
    }
}
